<template>
	<view>
		<u-navbar :border-bottom='false' back-icon-size='48' title="我的海报"></u-navbar>
		<view class="canvas">
			<canvas :style="{ width: cansWidth + 'px', height: cansHeight + 'px' }" canvas-id="firstCanvas"></canvas>
		</view>
		<view class="savehb" @click="saveCans(1)">
			<text>点击保存图片</text>
			<image src="../../../../static/imgs/xiazai.png" mode=""></image>
		</view>
		<view class="imglist">
			<view :style="[{ width:widths+'upx'}]">
				<block v-for="(swiperLists,index) in swiperList" :key="index">
					<image @click="selseimg(swiperLists.posterImg)" :src="swiperLists.posterImg" mode="">
					</image>
				</block>
				<view class="tuijian" @click='showWord = true'>
					<u-icon name="edit-pen" color="#333333" size="32"></u-icon>
					<text>推荐语</text>
				</view>
			</view>
		</view>

		<view class="shareBox">
			<view class="share-item" @click="shareWx">
				<image src="../../../../static/imgs/fengxiangdwx.png" mode=""></image>
				<text>分享到微信</text>
			</view>
			<view class="share-item" @click="copyUrlFn">
				<image src="../../../../static/imgs/fuzhilj.png" mode=""></image>
				<text>复制链接</text>
			</view>
		</view>

		<u-popup v-model="showWord" mode="center" border-radius="16" :closeable='true'>
			<view class="wordPop">
				<view class="title">
					推荐语
				</view>
				<view class="tip">
					分享至好友/微信群/朋友圈/QQ群， 附上推荐语更有吸引力
				</view>
				<view class="word">
					{{showContent}}
				</view>
				<view class="refresh" @click="exChange">
					<u-icon name="reload" color="#2979ff" size="28" style="margin-right: 10rpx;"></u-icon>换一条
				</view>
				<view class="copy" @click="copyFn">
					复制文案
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import md5 from '@/common/md5.js'
	import dokey from '@/common/dokey.js'
	import myRequest from '@/api/index.js'
	var that
	export default {
		data() {
			return {
				showWord: false,
				cansWidth: 299, //海报宽度
				cansHeight: 516, //海报高度
				widths: "", //图片列表动态宽度
				imgsrc: "", //保存图片的路径
				swiperList: [], //海报数组
				qrcode: '', // 二维码
				headPic: '', // 头像
				userdata: {},
				shareUrl: '',
				imgUrl: '',
				content:[
					'想不想免费打卡全球网红景点！赶紧扫码注册i世界vip吧。',
					'推荐好友扫码注册，推荐实体门店上线平台，你也会有钱赚哦！天天推荐，天天赚钱',
					'在i世界商城买买买，就送旅游基金，抵扣全球旅游，环游世界不是梦！',
					'想不想每个月加油都省钱！加入我们吧，1000元加油金免费送',
					'扫码注册vip，3000元福利金立马到手，旅行说走就走。',
					'想不想用更省钱的方法环游世界，跟我来吧，加入i世界，一起玩赚全球',
					'奈雪、kfc、星巴克等各大品牌优惠券任你淘，吃喝玩乐购样样都省钱！'
				],
				showContent:'想不想免费打卡全球网红景点！赶紧扫码注册i世界vip吧。'
			};
		},
		onLoad: function() {
			that = this
			that.getImgUrl()
			that.getShareUrl()
		},
		methods: {
			random(min, max) {
			  return Math.floor(Math.random() * (max - min)) + min;
			},
			// 换一条
			exChange(){
				let number = that.random(0,7)
				that.showContent = that.content[number]
			},
			// 获取分享链接
			async getShareUrl() {
				let res = await myRequest.getInvitecode()
				if (res.status) {
					that.shareUrl = res.url
				}
			},
			// 分享到微信
			shareWx() {
				if (that.imgUrl) {
					uni.share({
						provider: "weixin",
						scene: "WXSceneSession",
						type: 2,
						imageUrl: that.imgUrl,
						success: function(res) {
							console.log("success:" + JSON.stringify(res));
						},
						fail: function(err) {
							console.log("fail:" + JSON.stringify(err));
						}
					})
				} else {
					uni.showToast({
						title: '请先保存图片后,再分享到微信',
						icon: 'none'
					})
					return
				}

			},
			// 复制链接
			copyUrlFn() {
				uni.setClipboardData({
					data: that.shareUrl,
					success: function() {
						uni.showToast({
							title: '复制成功'
						})
					}
				})
			},
			// 图片地址
			async getImgUrl() {
				let config = {}
				config.token = uni.getStorageSync('token')
				config.show = '1'
				config.v = '1.1'
				let sortData = {} // 排序后的对象
				Object.keys(config).sort().map(key => {
					sortData[key] = config[key]
				})
				var secretdate = dokey.secretdate()
				var secretedOne = md5(JSON.stringify(sortData) + dokey.getkey() + secretdate)
				var secreted = md5(secretedOne) // 签名
				this.userdata.qrcode = 'https://api.loveworldzh.com/api/user/invitecode?auth_timestramp=' +
					secretdate +
					'&auth_sign=' + secreted + '&token=' + config.token + '&show=' + config.show + '&v=' + config.v

				let res = await myRequest.getUserInfo()
				if (res.status) {
					this.userdata.head = res.data.head_pic
					this.huizhihaibao()
				}
			},
			copyFn() {
				uni.setClipboardData({
					data: that.showContent,
					success: function() {
						uni.showToast({
							title: '复制成功'
						})
						that.showWord = false
					}
				})
			},

			huizhihaibao() {
				//数据请求结束
				uni.showLoading({
					title: '加载中'
				});
				that.swiperList = [{
						posterImg: "https://api.loveworldzh.com/uploads/jinlong/haibao1.png"
					},
					{
						posterImg: "https://api.loveworldzh.com/uploads/jinlong/haibao2.png"
					},
					{
						posterImg: "https://api.loveworldzh.com/uploads/jinlong/haibao3.png"
					},
					{
						posterImg: "https://api.loveworldzh.com/uploads/jinlong/haibao4.png"
					},
					{
						posterImg: "https://api.loveworldzh.com/uploads/jinlong/haibao5.png"
					}
				]

				uni.downloadFile({
					url: that.userdata.qrcode,
					success: function(res) {
						that.userdata.qrcode = res.tempFilePath
					}
				})

				uni.downloadFile({
					url: that.userdata.head,
					success: function(res) {
						that.userdata.head = res.tempFilePath
					}
				})

				uni.hideLoading();
				//数据请求结束
				if (that.userdata.head && that.userdata.qrcode) {
					that.selseimg(that.swiperList[0].posterImg)
				}
			},
			selseimg(e) { //绘制海报
				let that = this
				uni.showLoading({
					title: '生成海报中'
				});
				const context = uni.createCanvasContext('firstCanvas')

				uni.downloadFile({
					url: e,
					success: function(res) {
						context.drawImage(res.tempFilePath, 0, 0, 299, 516) //海报背景
					},
				})

				setTimeout(function() { //定时器，海报方法不能立即执行
					context.drawImage(that.userdata.qrcode, 188, 398, 84, 84) //二维码图片

					context.save()
					context.beginPath()
					context.arc(100, 390, 100, 0, 2 * Math.PI)
					context.clip()
					context.drawImage(that.userdata.head, 23, 393, 50, 50) //头像
					context.restore()
					context.draw()

					uni.hideLoading();
				}, 2000);

			},

			// 图片上传
			uploadImg(imgFiles) {
				uni.uploadFile({
					url: that.$baseURL + 'upload/up',
					filePath: imgFiles,
					name: 'file',
					success: (uploadFileRes) => {
						let obj = JSON.parse(uploadFileRes.data)
						if (obj.status) {
							that.imgUrl = obj.full_path
							console.log(that.imgUrl)
						}
					}
				});
			},

			saveCans(e) { //保存海报
				uni.showLoading({
					title: '保存海报中'
				});

				let tempRatio = 1.5;

				// #ifdef H5 || APP-PLUS
				tempRatio = 1;
				// #endif

				uni.canvasToTempFilePath({
					x: 0,
					y: 0,
					width: that.cansWidth * tempRatio,
					height: that.cansHeight * tempRatio,
					destWidth: that.cansWidth * tempRatio * 2,
					destHeight: that.cansHeight * tempRatio * 2,
					canvasId: 'firstCanvas',
					success: function(res) {
						uni.hideLoading()
						that.uploadImg(res.tempFilePath)

						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: function(red) {
								uni.showToast({
									title: '保存相册成功'
								})
								that.imgsrc = res.tempFilePath
							},
							fail(res) {
								if (res.errMsg == "saveImageToPhotosAlbum:fail auth deny") {
									uni.showModal({
										title: '您需要授权相册权限',
										success(res) {
											if (res.confirm) {
												uni.openSetting({
													success(res) {

													},
													fail(res) {
														console.log(res)
													}
												})
											}
										}
									})
								}
							}
						});
					},
					fail(res) {
						uni.hideLoading()
					}
				}, this)
			},


		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F1F1F1;
	}

	.savehb {
		width: 100%;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		image {
			width: 30rpx;
			height: 30rpx;
		}

		text {
			margin-right: 10rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
		}
	}

	.shareBox {
		padding-top: 20rpx;
		padding-bottom: 20rpx;
		display: flex;
		align-items: center;
		background-color: #FFFFFF;

		.share-item {
			flex: 1;
			height: 92rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 92rpx;
				height: 92rpx;
			}

			text {
				margin-left: 8rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #666666;
			}
		}
	}

	.tuijian {
		margin-left: auto;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-right: 20rpx;
	}

	.tuijian text {
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #333333;
	}

	.wordPop {
		width: 660rpx;
		height: auto;
		background: #FFFFFF;
		border-radius: 16rpx;
		padding-bottom: 40rpx;

		.title {
			width: 100%;
			height: 110rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: 600;
			color: #333333;
		}

		.tip {
			margin-top: 20rpx;
			padding: 0 140rpx;
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
			text-align: center;
		}

		.word {
			width: 584rpx;
			height: auto;
			background: #F2F2F7;
			border-radius: 8rpx;
			margin: 40rpx auto 0;
			padding: 20rpx 18rpx;
			line-height: 40rpx;
		}

		.refresh {
			width: 100%;
			text-align: center;
			margin-top: 20rpx;
			margin-bottom: 32rpx;
		}

		.copy {
			width: 440rpx;
			height: 88rpx;
			background: linear-gradient(180deg, #218BFF 0%, #3064FC 100%);
			border-radius: 16rpx;
			margin: 0 auto;
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			text-align: center;
			line-height: 88rpx;
		}
	}

	.pageHeader {
		height: var(--status-bar-height);
		background-color: #FFFFFF;
	}

	.box-header {
		position: fixed;
		top: 0upx;
		width: 100%;
		z-index: 100;
	}

	.top-box {
		height: 100upx;
		width: 100%;
	}

	.canvas {
		display: flex;
		justify-content: center;
	}

	.imglist {
		width: 100%;
		padding: 20upx 0 20upx 40upx;
		overflow: auto;
		background-color: #ffffff;
	}

	.imglist view {
		display: flex;
	}

	.imglist image {
		width: 86upx;
		height: 140upx;
		margin-right: 36rpx;
	}

	.tishi-box {
		display: flex;
		align-items: center;
		justify-content: space-around;
		background-color: #FFFFFF;
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 260upx;
		background: #FFFFFF;
		border-radius: 20upx 20upx 0upx 0upx;
		text-align: center;
	}

	.tishi-box view {
		font-size: 28upx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
		line-height: 50upx;
	}

	.tishi-box image {
		width: 100upx;
		height: 100upx;
		border-radius: 50%;
	}
</style>
